import * as THREE from "three";

function initParticle() {
    /*背景星星*/
    const particles = 1000;  //星星数量
    /*buffer做星星*/
    let bufferGeometry = new THREE.BufferGeometry();

    let positions = new Float32Array(particles * 3);

    const gap = 900; // 定义星星的最近出现位置

    for (let i = 0; i < positions.length; i += 3) {
        // positions
        /*-2gap < x < 2gap */
        let x = (Math.random() * gap * 2) * (Math.random() < .5 ? -1 : 1);
        let y = (Math.random() * gap * 2) * (Math.random() < .5 ? -1 : 1);
        let z = (Math.random() * gap * 2) * (Math.random() < .5 ? -1 : 1);

        /*找出x,y,z中绝对值最大的一个数*/
        let biggest = Math.abs(x) > Math.abs(y) ? Math.abs(x) > Math.abs(z) ? 'x' : 'z' :
            Math.abs(y) > Math.abs(z) ? 'y' : 'z';

        let pos = {x, y, z};

        /*如果最大值比n要小（因为要在一个距离之外才出现星星）则赋值为n（-n）*/
        if (Math.abs(pos[biggest]) < gap) pos[biggest] = pos[biggest] < 0 ? -gap : gap;

        x = pos['x'];
        y = pos['y'];
        z = pos['z'];

        positions[i] = x;
        positions[i + 1] = y;
        positions[i + 2] = z;
    }

    bufferGeometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
    bufferGeometry.computeBoundingSphere();
    /*星星的material*/
    let material = new THREE.PointsMaterial({ size: 3, color: 0xffffff, transparent: true });
    // 星空背景的星星闪烁效果
    function renderParticles() {
        material.opacity = 0.8 + Math.sin(Date.now() * 0.005) * 0.2;  // 动态改变透明度
        requestAnimationFrame(renderParticles);
    }
    
    renderParticles();
    const particleSystem = new THREE.Points(bufferGeometry, material);

    return particleSystem;
}

export default initParticle
